﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;

using ConfigDashboard.Models;

namespace ConfigDashboard.Controllers
{
    public class EnvironmentsController : Controller
    {
        private ConfigSettingsEntities db = new ConfigSettingsEntities();

        //
        // GET: /Environments/

        public ActionResult Index()
        {
            return View(db.ConfigEnvironments.ToList());
        }


        //
        // GET: /Environments/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Environments/Create

        [HttpPost]
        public ActionResult Create(ConfigEnvironment configenvironment)
        {
            if (ModelState.IsValid)
            {
                db.ConfigEnvironments.AddObject(configenvironment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(configenvironment);
        }

        //
        // GET: /Environments/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ConfigEnvironment configenvironment = db.ConfigEnvironments.Single(c => c.EnvironmentID == id);
            if (configenvironment == null)
            {
                return HttpNotFound();
            }
            return View(configenvironment);
        }

        //
        // POST: /Environments/Edit/5

        [HttpPost]
        public ActionResult Edit(ConfigEnvironment configenvironment)
        {
            if (ModelState.IsValid)
            {
                db.ConfigEnvironments.Attach(configenvironment);
                db.ObjectStateManager.ChangeObjectState(configenvironment, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(configenvironment);
        }

        //
        // GET: /Environments/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ConfigEnvironment configenvironment = db.ConfigEnvironments.Single(c => c.EnvironmentID == id);
            if (configenvironment == null)
            {
                return HttpNotFound();
            }
            return View(configenvironment);
        }

        //
        // POST: /Environments/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ConfigEnvironment configenvironment = db.ConfigEnvironments.Single(c => c.EnvironmentID == id);
            db.ConfigEnvironments.DeleteObject(configenvironment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        /// <summary>
        /// Releases unmanaged resources and optionally releases managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}